package com.qdsg.ysg.doctor.ui.activity.mine;

import android.widget.EditText;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.util.AES128;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 忘记密码 重新设置新密码
 * @Author: lzp
 * @CreateDate: 2022/7/1 10:15
 * @UpdateRemark: 更新说明
 */

public class ResetPwdActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_pwd_again)
    TextView etPwdAgain;
    @BindView(R.id.tv_achieve)
    TextView tvAchieve;

    String doctorCode;

    @Override
    protected void initViews() {
        tvTitle.setText("忘记密码");
    }

    @Override
    protected void initData() {
        doctorCode = getIntent().getStringExtra("doctorCode");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_reset_pwd;
    }


    @OnClick(R.id.tv_achieve)
    public void onViewClicked() {
        if (validate()) {
            modifyPassword();
        }
    }

    public boolean validate() {
        String pwd = etNewPwd.getText().toString();
        if (pwd.isEmpty()) {
            ToastUtil.showToast(mContext, "请输入新密码");
            return false;
        }
//        else if (!MatchUtil.verifyPassword(pwd)) {
//            ToastUtil.showToast(mContext, "密码只能包含字母、数字、下划线，且长度为6~16");
//            return false;
//        }
        else if (etPwdAgain.getText().toString().isEmpty()) {
            ToastUtil.showToast(mContext, "请再次输入密码");
            return false;
        } else if (!pwd.equals(etPwdAgain.getText().toString())) {
            ToastUtil.showToast(mContext, "两次密码输入不一致，请重新输入");
            etPwdAgain.setText("");
            return false;
        }
        return true;
    }

    private void modifyPassword() {
        showProgressDialog(mContext);
        try {
            RestProxy.getInstance().modifyPassword(AES128.encryptStringAES(doctorCode, "7t3e506jaa10xbd4"), AES128.encryptStringAES(etNewPwd.getText().toString(), "7t3e506jaa10xbd4"), new Observer<BaseResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseResponse baseResponse) {
                    finish();
                    ToastUtil.showToast(mContext, "修改密码成功");
                    dismissProgressDialog();
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(mContext, e);
                    dismissProgressDialog();
                }

                @Override
                public void onComplete() {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}
